<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 11:02
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Banner as BannerModel;


class Banner extends AdminBase{
    protected $user_model;
    protected $banner_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->banner_model = new BannerModel();
    }
    //banner列表
    public function index($page = 1)
    {
        $where = [];
        $params = input();
        if (!empty($params['title'])){
            $title = trim($params['title']);
            $where['title'] = ['like', "%$title%"];
        }
        $list = $this->banner_model->index($page, $where);
        return $this->fetch('index', ['list' => $list]);
    }

    //添加Banner页面
    public function add()
    {
        return $this->fetch('add');
    }

    //添加Banner
    public function addData()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'banner');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $params = input();
                $res = $this->banner_model->adddata($params);
                if (!$res) {
                    return $this->error('添加失败');
                }
                return $this->error('添加成功');
            }
        }
    }

    //获取Banner详情根据ID
    public function detail($id)
    {
        $detail = $this->banner_model->detail($id);
        return $this->fetch('edit', ['detail' => $detail]);
    }

    //Banner 启用
    public function updatestatus($id, $status)
    {
        //禁用
        if ($status == 1) {
            $res = $this->banner_model->edit($id, 2);
            if (!$res) {
                return  $this->error('禁用失败');
            } else {
                return $this->success('禁用成功');
            }
        }
        //解禁
        if ($status == 2) {
            $res = $this->banner_model->edit($id, 1);
            if (!$res) {
                return $this->error('解禁失败');
            } else {
                return $this->success('解禁成功');
            }
        }
    }

    //修改Banner
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'banner');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $params = input();
                $res = $this->banner_model->update($params);
                if (!$res) {
                    return $this->error('更新失败');
                }
                return $this->error('更新成功');
            }
        }

    }
}